<?php

declare(strict_types=1);

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/07/3
//      |_|                                         | TpMeCMS


namespace ImiApp\ApiServer\Api\Controller;


use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
use Phpben\Imi\Validate\Annotation\Validate;
// use Phpben\Imi\Auth\Annotation\Auth;
use ImiApp\ApiServer\Api\Controller\CommonController;


use ImiApp\ApiServer\Api\Controller\Tpmecmscom;
use EasyWeChat\Factory;
use Imi\Db\Db;

use Imi\Config;
use ImiApp\ApiServer\Common\Random;
// use fast\Random;


/**
 * 温馨提示：自己需要加入公共方法可以在app\common\controller\Tpmecmscom里面加，建议不要加入带有登录权限的
 * 或者继承该接口再加入自己的方法
 */



/**
 * 
 * 
 * @Controller("/tpmecms/")
 */
class Tpmecms extends Tpmecmscom
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $stores = null;

    /**微信相关  start
     *  ┏┻━━━━━━━━━┻┓
     *  ┃           ┃
     *  ┃ ┗┳     ┳┛ ┃
     *  ┃     ┻     ┃
     *  ┗━━━┓　┏━━━━┛
     *      ┃　┃
     *      ┃　┃
     *      ┃　┗━━━━━━━━━━┓
     *      ┃     He      ┣┓
     *      ┃　          ┏┛
     *      ┗━┓  ┏━━━┓  ┏┛
     *        ┗━━┛   ┗━━┛
     */



    /**
     * 获取支付app
     * @return void
     */
    protected function get_wx_pay_app()
    {
        $config = [
            'app_id' => Config('site.app_id'),
            'secret' => Config('site.secret'),
            // 'aes_key' => '',                    // EncodingAESKey，兼容与安全模式下请一定要填写！！！
            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'info', //info  'driver' => 'daily',
                // 'file' => __DIR__.'/wechat_order.log',
            ],

            'mch_id'     => Config('site.mch_id'), //商户号
            'key'        => Config('site.key'),   // API 密钥

            // 如需使用敏感接口（如退款、发送红包等）需要配置 API 证书路径(登录商户平台下载 API 证书)
            'cert_path'  => 'cert/apiclient_cert.pem', // XXX: 绝对路径！！！！
            'key_path'   => 'cert/apiclient_key.pem',      // XXX: 绝对路径！！！！
            // 'notify_url' => '默认的订单回调地址',     // 你也可以在下单时单独设置来想覆盖它


        ];

        $app = Factory::payment($config);
        return $app;
    }


    /**
     * 获取小程序
     * @return void
     */
    protected function get_wx_app()
    {
        $config = [
            'app_id' => Config('site.app_id'),
            'secret' => Config('site.secret'),
            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'driver' => 'daily',
                // 'level' => 'debug', //info  'driver' => 'daily', 
                // 'file' => __DIR__.'/wechat.log',
            ],
        ];

        $app = Factory::miniProgram($config);
        return $app;
    }

    /**
     * 获取微信公众号
     * @return void
     */
    protected function get_wx_gzh_app()
    {
        $config = [
            'app_id' => Config('site.wx_app_id'), //,
            'secret' => Config('site.wx_secret'), //,
            //  'token'   => Config('site.wx_secret'),//微信公众号->基本设置->服务器配置->令牌(Token)
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            //  'log' => [
            //     'level' => 'debug',
            //     'file'  => 'easywechat.log',
            // ],
            //...
        ];

        $app = Factory::officialAccount($config);

        return $app;
    }





    /**
     * 发送公众号模板消息测试
     * @param string $wx_openid 微信公众号openid
     * @param string $keyword1 自定义
     * @param string $keyword2 自定义
     * @param string $path
     * @return void
     */
    public function send_template_message_test($wx_openid = 'ooB8g6Hu3fa5P0oeMSpURFPVZRS4', $keyword1, $keyword2, $path = '/pages/u_xjdS/u_xjdS')
    {

        $app = $this->get_wx_gzh_app();
        $data = $app->template_message->send([
            'touser' => $wx_openid, //微信公众号openid,
            'template_id' => '_es-MdHZUO-voldjdevPADHj28JQ_t9bPzxowh_M8LM', //模板消息id
            'url' => 'https://he4966.cn/', //模板消息跳转地址
            'miniprogram' => [              //模板消息跳转小程序。小程序比网址优先级高，也可以注释其一
                'appid' => Config('site.app_id'),
                'pagepath' => $path,
            ],
            'data' => [
                'first' => ['您好，您有新的订单信息，请及时查看', '#173177'],
                'keyword1' => [$keyword1, '#173177'], //订单编号
                'keyword2' => [$keyword2, '#173177'], //订单时间
                // 'keyword3' =>[$service,'#173177'],
                'remark' => ['请尽快登入小程序查看', '#173177'],

            ],
        ]);
        return $data;
    }




    /**微信相关  end
     *  ┏┻━━━━━━━━━┻┓
     *  ┃           ┃
     *  ┃ ┗┳     ┳┛ ┃
     *  ┃     ┻     ┃
     *  ┗━━━┓　┏━━━━┛
     *      ┃　┃
     *      ┃　┃
     *      ┃　┗━━━━━━━━━━┓
     *      ┃     He      ┣┓
     *      ┃　          ┏┛
     *      ┗━┓  ┏━━━┓  ┏┛
     *        ┗━━┛   ┗━━┛
     */






    /**公共方法  start
     *  ┏┻━━━━━━━━━┻┓
     *  ┃           ┃
     *  ┃ ┗┳     ┳┛ ┃
     *  ┃     ┻     ┃
     *  ┗━━━┓　┏━━━━┛
     *      ┃　┃
     *      ┃　┃
     *      ┃　┗━━━━━━━━━━┓
     *      ┃     He      ┣┓
     *      ┃　          ┏┛
     *      ┗━┓  ┏━━━┓  ┏┛
     *        ┗━━┛   ┗━━┛
     */






    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | memo: 店铺积分的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/06/21
    //      |_|                                          | TpMeCMS

    /**
     * 减店铺的积分💰
     *
     * @param int $store_id 店铺id
     * @param float $score 积分
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function DecStoreScore($store_id, $score, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['before'] = $store['score']; //变更前积分
        $score = (float)$score;
        Db::name('store')->where('id', '=', $store_id)->setFieldDec('score', $score)->update(); //减去店铺积分

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['after'] = $store['score']; //变更后积分
        $data['store_id'] = $store_id;
        $data['score'] = $score;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入
        Db::name('store_score_log')->insert($data); //写入店铺score日志

    }

    /**
     * 加店铺的积分💰
     *
     * @param int $store_id 店铺id
     * @param float $score 积分
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function IncStoreScore($store_id, $score, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['before'] = $store['score']; //变更前积分
        $score = (float)$score;
        Db::name('store')->where('id', '=', $store_id)->setFieldInc('score', $score)->update(); //减去店铺积分

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['after'] = $store['score']; //变更后积分
        $data['store_id'] = $store_id;
        $data['score'] = $score;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入

        Db::name('store_score_log')->insert($data); //写入店铺score日志

    }


    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | memo: 店铺贝壳的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/06/21
    //      |_|                                          | TpMeCMS

    /**
     * 减店铺的贝壳💰
     *
     * @param int $store_id 店铺id
     * @param float $shell 贝壳
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function DecStoreShell($store_id, $shell, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['before'] = $store['shell']; //变更前贝壳
        $shell = (float)$shell;
        Db::name('store')->where('id', '=', $store_id)->setFieldDec('shell', $shell)->update(); //减去店铺贝壳

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['after'] = $store['shell']; //变更后贝壳
        $data['store_id'] = $store_id;
        $data['shell'] = $shell;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入
        Db::name('store_shell_log')->insert($data); //写入店铺shell日志

    }

    /**
     * 加店铺的贝壳💰
     *
     * @param int $store_id 店铺id
     * @param float $shell 贝壳
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function IncStoreShell($store_id, $shell, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['before'] = $store['shell']; //变更前贝壳
        $shell = (float)$shell;
        Db::name('store')->where('id', '=', $store_id)->setFieldInc('shell', $shell)->update(); //减去店铺贝壳

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['after'] = $store['shell']; //变更后贝壳
        $data['store_id'] = $store_id;
        $data['shell'] = $shell;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入

        Db::name('store_shell_log')->insert($data); //写入店铺shell日志

    }


    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | memo: 贝壳的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/31
    //      |_|                                          | TpMeCMS

    /**
     * 减用户的贝壳💰
     *
     * @param int $user_id 用户id
     * @param int $shell 贝壳
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    protected function DecUsershell($user_id, ?float $shell, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['before'] = $user['shell']; //变更前贝壳
        $shell = (float)$shell;
        Db::name('user')->where('id', '=', $user_id)->setFieldDec('shell', $shell)->update(); //减去用户贝壳

        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['after'] = $user['shell']; //变更后贝壳
        $data['user_id'] = $user_id;
        $data['shell'] = $shell;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入
        Db::name('user_shell_log')->insert($data); //写入用户shell日志

    }

    // /**
    //  * 加用户的贝壳💰
    //  *
    //  * @param int $user_id 用户id
    //  * @param int $shell 贝壳
    //  * @param string $memo 备注
    //  * @param string $typestatus_id 类型
    //  * @param int $public_id 公共id
    //  * @return void
    //  */
    // public function IncUsershell($user_id, ?float $shell, $memo = null, $typestatus_id = null, $public_id = null)
    // {
    //     $user = Db::name('user')->where('id', '=', $user_id)->find();
    //     $data['before'] = $user['shell']; //变更前贝壳
    //     $shell = (float)$shell;
    //     Db::name('user')->where('id', '=', $user_id)->setFieldInc('shell', $shell)->update(); //减去用户贝壳

    //     $user = Db::name('user')->where('id', '=', $user_id)->find();
    //     $data['after'] = $user['shell']; //变更后贝壳
    //     $data['user_id'] = $user_id;
    //     $data['shell'] = $shell;
    //     $data['memo'] = $memo;
    //     $data['createtime'] = time();
    //     $data['typestatus_id'] = $typestatus_id;
    //     $data['public_id'] = $public_id;
    //     $data['status'] = '2'; //状态:0=未知,1=支出,2=收入

    //     Db::name('user_shell_log')->insert($data); //写入用户shell日志

    // }

    /**
     * 加用户的贝壳💰
     *
     * @param int $user_id 用户id
     * @param int $shell 贝壳
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function IncUserShell($user_id, $shell, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['before'] = $user['shell']; //变更前贝壳
        $shell = (float)$shell;
        Db::name('user')->where('id', '=', $user_id)->setFieldInc('shell', $shell)->update(); //减去用户贝壳

        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['after'] = $user['shell']; //变更后贝壳
        $data['user_id'] = $user_id;
        $data['shell'] = $shell;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入

        Db::name('user_shell_log')->insert($data); //写入用户shell日志

    }

    




    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 运营中心积分的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/7/5
    //      |_|                                          | TpMeCMS

    /**
     * 减运营中心的余额积分💰
     *
     * @param int $run_id 运营中心id
     * @param int $score 积分
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function DecRunScore($run_id, $score, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['before'] = $run['score']; //变更前余额
        $score = (float)$score;
        Db::name('run')->where('id', '=', $run_id)->setFieldDec('score', $score)->update(); //减去运营中心余额

        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['after'] = $run['score']; //变更后余额
        $data['run_id'] = $run_id;
        $data['score'] = $score;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        Db::name('run_score_log')->insert($data); //写入运营中心score日志

    }



    /**
     * 加运营中心的余额积分💰
     *
     * @param int $run_id 运营中心id
     * @param int $score 积分
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncRunScore($run_id, $score, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['before'] = $run['score']; //变更前余额
        $score = (float)$score;
        Db::name('run')->where('id', '=', $run_id)->setFieldInc('score', $score)->update(); //减去运营中心余额

        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['after'] = $run['score']; //变更后余额
        $data['run_id'] = $run_id;
        $data['score'] = $score;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('run_score_log')->insert($data); //写入运营中心score日志

    }



    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 运营中心贝壳的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/7/5
    //      |_|                                          | TpMeCMS

    /**
     * 减运营中心的余额贝壳💰
     *
     * @param int $run_id 运营中心id
     * @param int $shell 贝壳
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function DecRunShell($run_id, $shell, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['before'] = $run['shell']; //变更前余额
        $shell = (float)$shell;
        Db::name('run')->where('id', '=', $run_id)->setFieldDec('shell', $shell)->update(); //减去运营中心余额

        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['after'] = $run['shell']; //变更后余额
        $data['run_id'] = $run_id;
        $data['shell'] = $shell;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        Db::name('run_shell_log')->insert($data); //写入运营中心shell日志

    }



    /**
     * 加运营中心的余额贝壳💰
     *
     * @param int $run_id 运营中心id
     * @param int $shell 贝壳
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncRunShell($run_id, $shell, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['before'] = $run['shell']; //变更前余额
        $shell = (float)$shell;
        Db::name('run')->where('id', '=', $run_id)->setFieldInc('shell', $shell)->update(); //减去运营中心余额

        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['after'] = $run['shell']; //变更后余额
        $data['run_id'] = $run_id;
        $data['shell'] = $shell;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('run_shell_log')->insert($data); //写入运营中心shell日志

    }


    public function getAfterSalesSite($area_id)
    {
        $site = Db::name('after_sales_site')->where('area_id', '=', $area_id)->find();
        return $site;
    }


    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 运营中心钱的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/07/5
    //      |_|                                          | TpMeCMS

    /**
     * 减运营中心的余额钱💰
     *
     * @param int $run_id 运营中心id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function DecRunMoney($run_id, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['before'] = $run['money']; //变更前余额
        $money = (float)$money;
        Db::name('run')->where('id', '=', $run_id)->setFieldDec('money', $money)->update(); //减去运营中心余额

        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['after'] = $run['money']; //变更后余额
        $data['run_id'] = $run_id;
        $data['money'] = $money;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        Db::name('run_money_log')->insert($data); //写入运营中心money日志

    }





    /**
     * 加运营中心的余额钱💰
     *
     * @param int $run_id 运营中心id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncRunMoney($run_id, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['before'] = $run['money']; //变更前余额
        $money = (float)$money;
        Db::name('run')->where('id', '=', $run_id)->setFieldInc('money', $money)->update(); //减去运营中心余额

        $run = Db::name('run')->where('id', '=', $run_id)->find();
        $data['after'] = $run['money']; //变更后余额
        $data['run_id'] = $run_id;
        $data['money'] = $money;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('run_money_log')->insert($data); //写入运营中心money日志

    }




    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 运营中心钱的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/07/5
    //      |_|                                          | TpMeCMS

    /**
     * 减运营中心的余额钱💰
     *
     * @param int $run_id 运营中心id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function DecRunExciteMoney($run_id, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $date = date('Y-m');
        $run = Db::name('run_excite_money')->where('run_id', '=', $run_id)->where('date','=',$date)->find();
        $money = (float)$money;
        if($run){
            $data['before'] = $run['money']; //变更前余额
            Db::name('run_excite_money')->where('run_id', '=', $run_id)->where('date','=',$date)->setFieldDec('money', $money)->update(); //减去运营中心余额
        }else{
            $data['before'] = 0; //变更前余额
            $data2['money'] = $money;
            $data2['run_id'] = $run_id;
            $data2['date'] = $date;
            $data2['year'] = date('Y');
            $data2['m'] = date('n');
            Db::name('run_excite_money')->insert($data2);
        }
       
        
    

        $run = Db::name('run_excite_money')->where('run_id', '=', $run_id)->where('date','=',$date)->find();
        $data['after'] = $run['money']; //变更后余额
        $data['run_id'] = $run_id;
        $data['money'] = $money;
        $data['memo'] = $date.'月'.$ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        Db::name('run_excite_money_log')->insert($data); //写入运营中心money日志

    }





    /**
     * 加运营中心的余额钱💰
     *
     * @param int $run_id 运营中心id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncRunExciteMoney($run_id, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $date = date('Y-m');
        $run = Db::name('run_excite_money')->where('run_id', '=', $run_id)->where('date','=',$date)->find();
        $money = (float)$money;
        if($run){
            $data['before'] = $run['money']; //变更前余额
            Db::name('run_excite_money')->where('run_id', '=', $run_id)->where('date','=',$date)->setFieldInc('money', $money)->update(); //加上运营中心余额
        }else{
            $data['before'] = 0; //变更前余额
            $data2['money'] = $money;
            $data2['run_id'] = $run_id;
            $data2['date'] = $date;
            $data2['year'] = date('Y');
            $data2['m'] = date('n');
            Db::name('run_excite_money')->insert($data2);
        }

        $run = Db::name('run_excite_money')->where('run_id', '=', $run_id)->where('date','=',$date)->find();
        $data['after'] = $run['money']; //变更后余额
        $data['run_id'] = $run_id;
        $data['money'] = $money;
        $data['memo'] = $date.'月'.$ps;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('run_excite_money_log')->insert($data); //写入运营中心money日志

    }





    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | memo: 积分的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/07/03
    //      |_|                                          | TpMeCMS

    /**
     * 减平台的积分💰
     *   
     * @param float $score 积分
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function DecSysinfoScore(?float $score, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['before'] = $sysinfo['score']; //变更前积分
        $score = (float)$score;
        Db::name('sysinfo')->where('id', '=', 1)->setFieldDec('score', $score)->update(); //减去平台积分

        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['after'] = $sysinfo['score']; //变更后积分
        $data['score'] = $score;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入
        Db::name('sysinfo_score_log')->insert($data); //写入平台score日志

    }

    /**
     * 加平台的积分💰
     *
     * @param float $score 积分
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function IncSysinfoScore(?float $score, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['before'] = $sysinfo['score']; //变更前积分
        $score = (float)$score;
        Db::name('sysinfo')->where('id', '=', 1)->setFieldInc('score', $score)->update(); //减去平台积分

        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['after'] = $sysinfo['score']; //变更后积分
        $data['score'] = $score;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入

        Db::name('sysinfo_score_log')->insert($data); //写入平台score日志

    }


    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | memo: 钱的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/07/03
    //      |_|                                          | TpMeCMS

    /**
     * 减平台的钱💰
     *   
     * @param float $money 钱
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function DecSysinfoMoney(?float $money, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['before'] = $sysinfo['money']; //变更前钱
        $money = (float)$money;
        Db::name('sysinfo')->where('id', '=', 1)->setFieldDec('money', $money)->update(); //减去平台钱

        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['after'] = $sysinfo['money']; //变更后钱
        $data['money'] = $money;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入
        Db::name('sysinfo_money_log')->insert($data); //写入平台money日志

    }

    /**
     * 加平台的钱💰
     *
     * @param float $money 钱
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function IncSysinfoMoney(?float $money, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['before'] = $sysinfo['money']; //变更前钱
        $money = (float)$money;
        Db::name('sysinfo')->where('id', '=', 1)->setFieldInc('money', $money)->update(); //减去平台钱

        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['after'] = $sysinfo['money']; //变更后钱
        $data['money'] = $money;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入

        Db::name('sysinfo_money_log')->insert($data); //写入平台money日志

    }



    //  _____       __  __         ____ __  __ ____         
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | memo: 贝壳的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/07/03
    //      |_|                                          | TpMeCMS

    /**
     * 减平台的贝壳💰
     *   
     * @param float $shell 贝壳
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function DecSysinfoShell(?float $shell, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['before'] = $sysinfo['shell']; //变更前贝壳
        $shell = (float)$shell;
        Db::name('sysinfo')->where('id', '=', 1)->setFieldDec('shell', $shell)->update(); //减去平台贝壳

        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['after'] = $sysinfo['shell']; //变更后贝壳
        $data['shell'] = $shell;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入
        Db::name('sysinfo_shell_log')->insert($data); //写入平台shell日志

    }



    /**
     * 加平台的贝壳💰
     *
     * @param float $shell 贝壳
     * @param string $memo 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    public function IncSysinfoShell(?float $shell, $memo = null, $typestatus_id = null, $public_id = null)
    {
        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['before'] = $sysinfo['shell']; //变更前贝壳
        $shell = (float)$shell;
        Db::name('sysinfo')->where('id', '=', 1)->setFieldInc('shell', $shell)->update(); //减去平台贝壳

        $sysinfo = Db::name('sysinfo')->where('id', '=', 1)->find();
        $data['after'] = $sysinfo['shell']; //变更后贝壳
        $data['shell'] = $shell;
        $data['memo'] = $memo;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入

        Db::name('sysinfo_shell_log')->insert($data); //写入平台shell日志

    }


    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 钱的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/31
    //      |_|                                          | TpMeCMS

    /**
     * 减用户的余额钱💰
     *
     * @param int $uid 用户id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    protected function DecUserMoney($uid, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $set = [1 => '换客', 2 => '拼客', 3 => '拍客', 4 => '创客', 5 => '合伙人', 6 => '运营中心', 7 => '售后站', 10 => '普通个人', 11 => '金牌个人', 12 => '皇冠个人', 13 => '普通企业', 14 => '金牌企业', 15 => '皇冠企业'];

        $user = Db::name('user')->where('id', '=', $uid)->find();
        $str  = $set[$user['set_id']];
        $data['before'] = $user['money']; //变更前余额
        $money = (float)$money;
        Db::name('user')->where('id', '=', $uid)->setFieldDec('money', $money)->update(); //减去用户余额

        $user = Db::name('user')->where('id', '=', $uid)->find();
        $data['after'] = $user['money']; //变更后余额
        $data['user_id'] = $uid;
        $data['money'] = $money;
        $data['memo'] = $str . $ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        Db::name('user_money_log')->insert($data); //写入用户money日志

    }

    /**
     * 加用户的余额钱💰
     *
     * @param int $uid 用户id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function inc_user_money($uid, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $this->IncUserMoney($uid, $money, $ps, $typestatus_id, $public_id);
    }



    /**
     * 获取用户信息
     *
     * @param int $uid 用户id
     * @return void
     */
    public function getUserData($user_id)
    {
        $user = Db::name('user')->where('id', '=', $user_id)->find();
        return $user;
    }


    /**
     * 加用户的余额钱💰
     *
     * @param int $uid 用户id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncUserMoney($uid, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $set = [1 => '换客', 2 => '拼客', 3 => '拍客', 4 => '创客', 5 => '合伙人', 6 => '运营中心', 7 => '售后站', 10 => '普通个人', 11 => '金牌个人', 12 => '皇冠个人', 13 => '普通企业', 14 => '金牌企业', 15 => '皇冠企业'];
        

        $user = Db::name('user')->where('id', '=', $uid)->find();
        var_dump($uid, $money, $ps , $typestatus_id , $public_id,$user);
        $str  = $set[$user['set_id']];

        $data['before'] = $user['money']; //变更前余额
        $money = (float)$money;
        Db::name('user')->where('id', '=', $uid)->setFieldInc('money', $money)->update(); //减去用户余额

        $user = Db::name('user')->where('id', '=', $uid)->find();
        $data['after'] = $user['money']; //变更后余额
        $data['user_id'] = $uid;
        $data['money'] = $money;
        $data['memo'] = $str . $ps;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('user_money_log')->insert($data); //写入用户money日志

    }



    /**
     * 加用户的提成钱💰
     *
     * @param int $user_id 用户id
     * @param int $commission 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncUserCommission($user_id, $commission, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $set = [1 => '换客', 2 => '拼客', 3 => '拍客', 4 => '创客', 5 => '合伙人', 6 => '运营中心', 7 => '售后站', 10 => '普通个人', 11 => '金牌个人', 12 => '皇冠个人', 13 => '普通企业', 14 => '金牌企业', 15 => '皇冠企业'];
        

        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $str  = $set[$user['set_id']];

        $data['before'] = $user['commission']; //变更前余额
        $commission = (float)$commission;
        Db::name('user')->where('id', '=', $user_id)->setFieldInc('commission', $commission)->update(); //减去用户余额

        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['after'] = $user['commission']; //变更后余额
        $data['user_id'] = $user_id;
        $data['commission'] = $commission;
        $data['memo'] = $str .'👈🏻'. $ps;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('user_commission_log')->insert($data); //写入用户commission日志

    }


    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 店铺钱的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/06/6
    //      |_|                                          | TpMeCMS

    /**
     * 减店铺的余额钱💰
     *
     * @param int $store_id 店铺id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function DecStoreMoney($store_id, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['before'] = $store['money']; //变更前余额
        $money = (float)$money;
        Db::name('store')->where('id', '=', $store_id)->setFieldDec('money', $money)->update(); //减去店铺余额

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['after'] = $store['money']; //变更后余额
        $data['store_id'] = $store_id;
        $data['money'] = $money;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        Db::name('store_money_log')->insert($data); //写入店铺money日志

    }



    /**
     * 加店铺的余额钱💰
     *
     * @param int $store_id 店铺id
     * @param int $money 钱
     * @param string $ps 备注
     * @param mixed $typestatus_id 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncStoreMoney($store_id, $money, $ps = null, $typestatus_id = 0, $public_id = null)
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['before'] = $store['money']; //变更前余额
        $money = (float)$money;
        Db::name('store')->where('id', '=', $store_id)->setFieldInc('money', $money)->update(); //减去店铺余额

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['after'] = $store['money']; //变更后余额
        $data['store_id'] = $store_id;
        $data['money'] = $money;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['typestatus_id'] = $typestatus_id;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('store_money_log')->insert($data); //写入店铺money日志

    }




    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 店铺保证金的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/06/27
    //      |_|                                          | TpMeCMS

    /**
     * 减店铺的余额保证金💰
     *
     * @param int $store_id 店铺id
     * @param float $surety 保证金
     * @param string $ps 备注
     * @param mixed $type_status 类型:act_surety=活动保证金,surety=保证金
     * @param string $type 类型
     * @param mixed $public_id id
     * @return void
     */
    protected function DecStoreSurety($store_id, $surety, $ps = null, $type = '', $public_id = null, $type_status = 'surety')
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();

        $data['before'] = $store[$type_status]; //变更前余额
        $surety = (float)$surety;
        Db::name('store')->where('id', '=', $store_id)->setFieldDec($type_status, $surety)->update(); //减去店铺余额

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['type'] = $type;
        $data['after'] = $store[$type_status]; //变更后余额
        $data['store_id'] = $store_id;
        $data[$type_status] = $surety;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['type_status'] = $type_status;
        $data['public_id'] = $public_id;
        Db::name('store_surety_log')->insert($data); //写入店铺surety日志

    }



    /**
     * 加店铺的余额保证金💰
     *
     * @param int $store_id 店铺id
     * @param float $surety 保证金
     * @param string $ps 备注
     * @param mixed $type_status 类型:act_surety=活动保证金,surety=保证金
     * @param string $type 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncStoreSurety($store_id, $surety, $ps = null, $type = '', $public_id = null, $type_status = 'surety')
    {
        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['before'] = $store[$type_status]; //变更前余额
        $surety = (float)$surety;
        Db::name('store')->where('id', '=', $store_id)->setFieldInc($type_status, $surety)->update(); //减去店铺余额

        $store = Db::name('store')->where('id', '=', $store_id)->find();
        $data['type'] = $type;
        $data['after'] = $store[$type_status]; //变更后余额
        $data['store_id'] = $store_id;
        $data[$type_status] = $surety;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['type_status'] = $type_status;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        Db::name('store_surety_log')->insert($data); //写入店铺surety日志

    }





    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 用户保证金的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/06/28
    //      |_|                                          | TpMeCMS

    /**
     * 减用户的保证金💰
     *
     * @param int $user_id 用户id
     * @param float $surety 保证金
     * @param string $ps 备注
     * @param mixed $type_status 类型:act_surety=活动保证金,surety=保证金
     * @param string $type 类型
     * @param mixed $public_id id
     * @return void
     */
    protected function DecUserSurety($user_id, $surety, $ps = null, $type = '', $public_id = null, $type_status = 'surety')
    {
        $user = Db::name('user')->where('id', '=', $user_id)->find();

        $data['before'] = $user[$type_status]; //变更前保证金
        $surety = (float)$surety;
        Db::name('user')->where('id', '=', $user_id)->setFieldDec($type_status, $surety)->update(); //减去用户保证金

        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['type'] = $type;
        $data['after'] = $user[$type_status]; //变更后保证金
        $data['user_id'] = $user_id;
        $data[$type_status] = $surety;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['status'] = '1'; //状态:0=未知,1=支出,2=收入

        $data['type_status'] = $type_status;
        $data['public_id'] = $public_id;
        Db::name('user_surety_log')->insert($data); //写入用户surety日志

    }



    /**
     * 加用户的保证金💰
     *
     * @param int $user_id 用户id
     * @param float $surety 保证金
     * @param string $ps 备注
     * @param mixed $type_status 类型:act_surety=活动保证金,surety=保证金
     * @param string $type 类型
     * @param mixed $public_id id
     * @return void
     */
    public function IncUserSurety($user_id, $surety, $ps = null, $type = '', $public_id = null, $type_status = 'surety')
    {
        $user = Db::name('user')->where('id', '=', $user_id)->find();
        var_dump('用户', $user);
        $data['before'] = $user[$type_status]; //变更前保证金
        $surety = (float)$surety;
        Db::name('user')->where('id', '=', $user_id)->setFieldInc($type_status, $surety)->update(); //减去用户保证金

        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['type'] = $type;
        $data['after'] = $user[$type_status]; //变更后保证金
        $data['user_id'] = $user_id;
        $data[$type_status] = $surety;
        $data['memo'] = $ps;
        $data['createtime'] = time();
        $data['type_status'] = $type_status;
        $data['public_id'] = $public_id;
        $data['status'] = '2'; //状态:0=未知,1=支出,2=收入
        var_dump($data);
        Db::name('user_surety_log')->insert($data); //写入用户surety日志

    }



    //  _____       __  __         ____ __  __ ____  
    // |_   __ __  |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
    //   | || '_ \ | |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
    //   | || |_)| | |  | |  __/ | |___| |  | |___) |    | PS: 积分的操作及日志
    //   |_|| .__/ |_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/31
    //      |_|                                          | TpMeCMS

    /**
     * 减用户的积分💎
     *
     * @param int $user_id 用户id
     * @param int $score 积分
     * @param string $ps 备注
     * @param string $typestatus_id 类型
     * @param int $public_id 公共id
     * @return void
     */
    protected function DecUserScore($user_id, $score, $ps = null, $typestatus_id = null, $public_id = null)
    {
        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['before'] = $user['score']; //变更前积分
        $score = (float)$score;
        Db::name('user')->where('id', '=', $user_id)->setFieldDec('score', $score)->update(); //减去用户积分

        $user = Db::name('user')->where('id', '=', $user_id)->find();
        $data['after'] = $user['score']; //变更后积分
        $data['user_id'] = $user_id;
        $data['score'] = $score;
        $data['memo'] = (string)$ps;
        $data['status'] = '1';
        $data['createtime'] = time();
        $data['typestatus_id'] = (string)$typestatus_id;
        $data['public_id'] = (int)$public_id;


        $id = Db::name('user_score_log')->insert($data)->getLastInsertId(); //写入用户score日志

        //减用户积分表里面的积分
        $this->DecUserScoreSelf($user_id, $score, $id, $ps);
    }

    /**
     * 减用户的积分💎
     *
     * @param int $user_id 用户id
     * @param int $score 积分
     * @param string $id 记录id
     * @param string $ps 记录ps
     * @return void
     */
    protected function DecUserScoreSelf($user_id, $score, $id = null, $ps = null)
    {
        //status 状态:1=待使用,3=已使用,4=已过期
        $list = Db::name('user_score')->where('user_id', '=', $user_id)->where('status', '=', 1)->orderRaw('expire_time asc')->select()->getArray();

        $待扣积分 =  (float)$score;
        $ids = [];
        foreach ($list as $key => $val) {

            if ($待扣积分 > 0) {
                if ($待扣积分 <= $val['score']) {
                    //扣完了 可能有剩余

                    $data = [];
                    $剩余积分 =   $val['score'] - $待扣积分;
                    $待扣积分 = 0;
                    $data['score'] = $剩余积分;
                    if ($剩余积分 <= 0) {
                        $data['status'] = '3'; //状态:1=待使用,3=已使用,4=已过期
                    }

                    $log = json_decode($val['log'], true);
                    $log[] = ['date' => date('Y-m-d H:i:s'), 'status' => '1', 'user_score_log_id' => $id, '总共要扣积分' => $score, '当前积分' => $val['score'], '减去积分' => $待扣积分, '备注' => $ps];
                    $data['log'] = json_encode($log, JSON_UNESCAPED_UNICODE);

                    $ids[] = $val['id'];
                    Db::name('user_score')->where('id', '=',  $val['id'])->update($data);
                } else {
                    //未扣完
                    $data = [];
                    $待扣积分 = $待扣积分 - $val['score'];
                    $data['score'] = 0;
                    $data['status'] = '3';

                    $log = json_decode($val['log'], true);
                    $log[] = ['date' => date('Y-m-d H:i:s'), 'status' => '1', 'user_score_log_id' => $id, '总共要扣积分' => $score, '当前积分' => $val['score'], '减去积分' => $val['score'], '还要扣' => $待扣积分, '备注' => $ps];
                    $data['log'] = json_encode($log, JSON_UNESCAPED_UNICODE);
                    
                    $ids[] = $val['id'];

                    Db::name('user_score')->where('id', '=', $val['id'])->update($data);
                }
            } else {
                break;
            }
        }
    }

    /**
     * 加用户的积分💎
     *
     * @param int $uid 用户id
     * @param int $score 积分
     * @param string $ps 备注
     * @param string $typestatus_id 类型:2=邀请用户,3=大转盘购买,4=大转盘中奖,6=每日签到
     * @param int $public_id 公共id
     * @return void
     */
    protected function IncUserScore($uid, $score, $ps = null, $typestatus_id = null, $public_id = null)
    {
        $user = Db::name('user')->where('id', '=', $uid)->find();
        $data['before'] = $user['score']; //变更前积分
        $score = (float)$score;
        Db::name('user')->where('id', '=', $uid)->setFieldInc('score', $score)->update(); //减去用户积分

        $user = Db::name('user')->where('id', '=', $uid)->find();
        $data['after'] = $user['score']; //变更后积分
        $data['user_id'] = $uid;
        $data['score'] = $score;
        $data['memo'] = $ps;
        $data['status'] = '2';
        $data['createtime'] = time();
        $data['typestatus_id'] = (int)$typestatus_id;
        $data['public_id'] = (int)$public_id;
        $id = Db::name('user_score_log')->insert($data)->getLastInsertId(); //写入用户score日志

        $data2['user_id'] = $uid;
        $data2['score'] = $score;
        $data2['sum_score'] = $score;
        $data2['createtime'] = $data['createtime'];
        $data2['expire_time'] = $data['createtime'] + 86400 * config('score_expire_day');
        $data2['log'] = json_encode([['date' => date('Y-m-d H:i:s'), 'status' => '2', 'user_score_log_id' => $id, '备注' => $ps]], JSON_UNESCAPED_UNICODE);
        Db::name('user_score')->insert($data2);
    }




    /**
     * 生成订单号
     *
     * @param string $qian 前缀
     * @param string $uid 用户id
     * @param int $len 订单长度
     * @return void
     */
    protected function rand_order($qian = null, $uid = null, $len = 32)
    {
        if (strlen($qian . $uid) < 10) {
            $str = implode(NULL, array_map('ord', str_split(substr(Random::uuid(), 7, 13), 1)));
            //.Random::uuid();
            if ($uid) {
                $order = date('YmdHis') . $uid;
            } else {
                // if($this->auth->id){//需要登录
                //     $order = $this->auth->id.$order;
                // }
                $order = date('YmdHis');
            }
        } else {
            $str = implode(NULL, array_map('ord', str_split(substr(Random::uuid(), 7, 13), 1)));
            $order = date('is') . $uid;
        }

        if ($qian) {
            $order = $qian . $order;
        }
        if ($len - strlen($order) > 0) {
            // var_dump('order',$order);
            $order = $order . substr($str, 1, $len - strlen($order));
        } else {
            $order = substr($order, -32);
        }

        return $order;
    }



    /**
     * 小程序openid获取用户信息
     * @param string $openid
     * @return void
     */
    public function openid_get_user($openid)
    {
        $user =  Db::name('user')->where('openid', $openid)->find();
        return $user;
    }


    /**
     * 微信公众号wx_openid获取用户信息
     * @param string $wx_openid
     * @return void
     */
    public function wx_openid_get_user($openid)
    {
        $user =  Db::name('user')->where('wx_openid', $openid)->find();
        return $user;
    }


    /**
     * 获取用户的微信公众号openid
     * 
     * @param array $uids 用户ids
     * @return void
     */
    public function get_user_wx_openids($uids = null)
    {
        if (is_array($uids)) {
            $list = Db::name('user')->where('id', 'in', $uids)->column('wx_openid');
        } else {
            $list = Db::name('user')->where('id', $uids)->value('wx_openid');
        }

        return $list;
    }







    /**
     * 更新店铺坐标
     * @param int $store_id 店铺id
     * @return void
     */
    public function update_store_geo($store_id)
    {
        $store = $this->verify_store_id($store_id);
        $geo = new \addons\redisgeo\controller\Index(); //需要装redisgeo插件，需要联系QQ496631085
        $geo->add_geo($store['lng'], $store['lat'], $store['id']);
    }



    /**
     * 计算坐标距离
     * @param $lng1 经度1
     * @param $lat1 纬度1
     * @param $lng2 经度2
     * @param $lat2 纬度2
     * @return float
     */
    public function get_coordinate_distance($lng1 = null, $lat1 = null, $lng2 = null, $lat2 = null)
    {
        if ($lng1 && $lng2 && $lat1 && $lat2) {
            $radLat1 = deg2rad((float)$lat1); //deg2rad((float))函数将角度转换为弧度
            $radLat2 = deg2rad((float)$lat2);
            $radLng1 = deg2rad((float)$lng1);
            $radLng2 = deg2rad((float)$lng2);
            $a = $radLat1 - $radLat2;
            $b = $radLng1 - $radLng2;
            $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;

            return round($s, 2);
        } else {
            return 0;
        }
    }








    /**公共方法  end
     *  ┏┻━━━━━━━━━┻┓
     *  ┃           ┃
     *  ┃ ┗┳     ┳┛ ┃
     *  ┃     ┻     ┃
     *  ┗━━━┓　┏━━━━┛
     *      ┃　┃
     *      ┃　┃
     *      ┃　┗━━━━━━━━━━┓
     *      ┃     He      ┣┓
     *      ┃　          ┏┛
     *      ┗━┓  ┏━━━┓  ┏┛
     *        ┗━━┛   ┗━━┛
     */







    /** 验证
     * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
     * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
     * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
     * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
     * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│- _│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
     * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
     * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │[ {│] }│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
     * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
     * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │; :│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
     * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
     * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
     * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
     * │ Ctrl│    │Alt │ Tpmecms   store       │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
     * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
     */





    /** 验证
     * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
     * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
     * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
     * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
     * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│- _│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
     * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
     * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │[ {│] }│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
     * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
     * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │; :│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
     * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
     * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
     * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
     * │ Ctrl│    │Alt │ Tpmecms   store       │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
     * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
     */




    /**
     * 加密字符串(可自行配对修改)
     *
     * @param string $str
     * @return void
     */
    protected function enStr($str)
    {

        $str = base64_encode(base64_encode($str));
        return $str;
    }

    /**
     * 解密字符串(可自行配对修改)
     *
     * @param string $str
     * @return void
     */
    protected function deStr($str)
    {

        $str = base64_decode(base64_decode($str));
        return $str;
    }



    /**
     * 获取字段数组（可处理时间戳）
     *
     * @param array $arr 数组
     * @param array $fields 需要的字段
     * @param boolean $time 是否转化时间戳
     * @return void
     */
    protected function get_field_arr($arr, $fields, $time = false)
    {
        if (!is_array($fields)) {
            //字符串
            $fields = explode(',', $fields);
        }

        $new = array();
        foreach ($fields as $key => $val) {
            if (isset($arr[$val])) {
                //字段后缀未time的可转时间戳
                if ($time && strlen($val) > 3 && substr($val, -4) == 'time') {

                    $new[$val] = strtotime($arr[$val]);
                } else {
                    $new[$val] = $arr[$val];
                }
            }
        }
        return $new;
    }
}
